<template>
  <div class="p-6 bg-base-200/50 rounded-2xl">
    <CommentItem
      :comment="comment"
      :replies="replies"
      :show-replies="true"
      @toggle-like="handleToggleLike"
      @create-reply="handleCreateReply"
      @report-comment="handleReportComment"
      @delete-comment="handleDeleteComment"
      @reply-click="handleReplyClick"
    >
      <!-- 自定义头像插槽 -->
      <template #avatar="{ author, sizeClass }">
        <div class="relative group">
          <div
            :class="[
              'rounded-full overflow-hidden border-4 border-primary shadow-lg cursor-pointer transform hover:scale-110 transition-all duration-300',
              sizeClass,
            ]"
            @click="showUserDetail(author)"
          >
            <img :src="author.avatar" :alt="author.name" class="w-full h-full object-cover" />
          </div>
          <!-- 在线状态指示器 -->
          <div
            class="absolute -bottom-1 -right-1 w-4 h-4 bg-success rounded-full border-2 border-base-100 shadow-sm"
          ></div>
          <!-- 悬停提示 -->
          <div
            class="absolute -top-12 left-1/2 transform -translate-x-1/2 bg-base-content text-base-100 text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity duration-200 whitespace-nowrap"
          >
            点击查看 {{ author.name }} 的资料
          </div>
        </div>
      </template>
    </CommentItem>

    <!-- 用户详情弹窗 -->
    <div
      v-if="showUserPanel"
      class="fixed inset-0 bg-black/50 backdrop-blur-sm z-50 flex items-center justify-center p-4"
      @click="closeUserDetail"
    >
      <div
        class="bg-base-100 rounded-2xl shadow-2xl max-w-md w-full max-h-[80vh] overflow-hidden transform transition-all duration-300"
        :class="showUserPanel ? 'scale-100 opacity-100' : 'scale-95 opacity-0'"
        @click.stop
      >
        <!-- 弹窗头部 -->
        <div
          class="bg-gradient-to-r from-primary/10 to-secondary/10 px-6 py-4 border-b border-base-300/30"
        >
          <div class="flex items-center justify-between">
            <h3 class="text-lg font-semibold text-base-content">用户资料</h3>
            <button
              @click="closeUserDetail"
              class="btn btn-sm btn-ghost btn-circle hover:bg-base-200"
            >
              <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path
                  stroke-linecap="round"
                  stroke-linejoin="round"
                  stroke-width="2"
                  d="M6 18L18 6M6 6l12 12"
                />
              </svg>
            </button>
          </div>
        </div>

        <!-- 弹窗内容 -->
        <div class="p-6 overflow-y-auto max-h-[60vh]">
          <div v-if="selectedUser" class="space-y-6">
            <!-- 用户头像和基本信息 -->
            <div class="text-center">
              <div class="relative inline-block">
                <img
                  :src="selectedUser.avatar"
                  :alt="selectedUser.name"
                  class="w-24 h-24 rounded-full border-4 border-primary shadow-lg mx-auto"
                />
                <div
                  class="absolute -bottom-1 -right-1 w-6 h-6 bg-success rounded-full border-2 border-base-100"
                ></div>
              </div>
              <h4 class="text-xl font-bold text-base-content mt-4">{{ selectedUser.name }}</h4>
              <p class="text-base-content/70 text-sm">用户ID: {{ selectedUser.id }}</p>
            </div>

            <!-- 用户统计信息 -->
            <div class="grid grid-cols-3 gap-4">
              <div class="text-center p-3 bg-base-200/50 rounded-lg">
                <div class="text-2xl font-bold text-primary">{{ userStats.comments }}</div>
                <div class="text-xs text-base-content/70">评论数</div>
              </div>
              <div class="text-center p-3 bg-base-200/50 rounded-lg">
                <div class="text-2xl font-bold text-secondary">{{ userStats.likes }}</div>
                <div class="text-xs text-base-content/70">获赞数</div>
              </div>
              <div class="text-center p-3 bg-base-200/50 rounded-lg">
                <div class="text-2xl font-bold text-accent">{{ userStats.followers }}</div>
                <div class="text-xs text-base-content/70">关注者</div>
              </div>
            </div>

            <!-- 用户标签 -->
            <div>
              <h5 class="text-sm font-semibold text-base-content mb-3">标签</h5>
              <div class="flex flex-wrap gap-2">
                <span class="px-3 py-1 bg-primary/10 text-primary text-xs rounded-full font-medium"
                  >活跃用户</span
                >
                <span
                  class="px-3 py-1 bg-secondary/10 text-secondary text-xs rounded-full font-medium"
                  >技术专家</span
                >
                <span class="px-3 py-1 bg-accent/10 text-accent text-xs rounded-full font-medium"
                  >社区贡献者</span
                >
              </div>
            </div>

            <!-- 最近活动 -->
            <div>
              <h5 class="text-sm font-semibold text-base-content mb-3">最近活动</h5>
              <div class="space-y-2">
                <div class="flex items-center gap-3 p-2 bg-base-200/30 rounded-lg">
                  <div class="w-2 h-2 bg-primary rounded-full"></div>
                  <span class="text-sm text-base-content/80">发表了新评论</span>
                  <span class="text-xs text-base-content/60 ml-auto">2小时前</span>
                </div>
                <div class="flex items-center gap-3 p-2 bg-base-200/30 rounded-lg">
                  <div class="w-2 h-2 bg-secondary rounded-full"></div>
                  <span class="text-sm text-base-content/80">点赞了文章</span>
                  <span class="text-xs text-base-content/60 ml-auto">5小时前</span>
                </div>
                <div class="flex items-center gap-3 p-2 bg-base-200/30 rounded-lg">
                  <div class="w-2 h-2 bg-accent rounded-full"></div>
                  <span class="text-sm text-base-content/80">关注了新用户</span>
                  <span class="text-xs text-base-content/60 ml-auto">1天前</span>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 弹窗底部 -->
        <div class="px-6 py-4 bg-base-200/30 border-t border-base-300/30 flex gap-3">
          <button
            @click="followUser"
            class="btn btn-primary btn-sm flex-1"
            :class="{ 'btn-outline': !isFollowing }"
          >
            {{ isFollowing ? '已关注' : '关注' }}
          </button>
          <button @click="sendMessage" class="btn btn-secondary btn-sm flex-1">私信</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { CommentItem } from '@/components/common/comment'
import type { Comment } from '@/apis/methods/comment.api'

// 评论数据
const comment = ref<Comment>({
  id: '1',
  content:
    '这个示例展示了自定义头像插槽的用法。头像有边框、阴影、悬停缩放效果，还有在线状态指示器和悬停提示。',
  author: {
    id: '1',
    name: '头像专家',
    avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=avatar',
  },
  createdAt: '2024-01-15T10:30:00Z',
  updatedAt: '2024-01-15T10:30:00Z',
  likes: 15,
  isLiked: false,
  replyCount: 2,
  parentId: undefined,
  articleId: '1',
  mentionedUsers: [],
  status: 'approved',
})

// 回复数据
const replies = ref<Comment[]>([
  {
    id: 'r1',
    content: '头像设计得很棒！',
    author: {
      id: '2',
      name: '设计师',
      avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=designer',
    },
    createdAt: '2024-01-15T11:00:00Z',
    updatedAt: '2024-01-15T11:00:00Z',
    likes: 3,
    isLiked: false,
    replyCount: 0,
    parentId: '1',
    articleId: '1',
    mentionedUsers: [],
    status: 'approved',
  },
])

// 事件处理函数
const handleToggleLike = (commentId: string) => {
  const targetComment =
    comment.value.id === commentId ? comment.value : replies.value.find((c) => c.id === commentId)
  if (targetComment) {
    targetComment.isLiked = !targetComment.isLiked
    targetComment.likes += targetComment.isLiked ? 1 : -1
  }
}

const handleCreateReply = (parentId: string, content: string) => {
  console.log('创建回复:', { parentId, content })
}

const handleReportComment = (commentId: string) => {
  console.log('举报评论:', commentId)
}

const handleDeleteComment = (commentId: string) => {
  console.log('删除评论:', commentId)
}

// 用户详情弹窗相关状态
const showUserPanel = ref(false)
const selectedUser = ref<Comment['author'] | null>(null)
const isFollowing = ref(false)

// 用户统计数据
const userStats = ref({
  comments: 42,
  likes: 128,
  followers: 89,
})

// 显示用户详情
const showUserDetail = (author: Comment['author']) => {
  selectedUser.value = author
  showUserPanel.value = true
  // 模拟检查关注状态
  isFollowing.value = Math.random() > 0.5
}

// 关闭用户详情
const closeUserDetail = () => {
  showUserPanel.value = false
  selectedUser.value = null
}

// 关注用户
const followUser = () => {
  isFollowing.value = !isFollowing.value
  console.log(isFollowing.value ? '关注用户' : '取消关注', selectedUser.value?.name)
}

// 发送私信
const sendMessage = () => {
  console.log('发送私信给', selectedUser.value?.name)
  // 这里可以打开私信对话框
}

const handleReplyClick = (reply: Comment) => {
  console.log('点击回复:', reply)
}
</script>

<style scoped>
/* 自定义头像样式 */

/* 弹窗动画 */
.user-panel-enter-active,
.user-panel-leave-active {
  transition: all 0.3s ease;
}

.user-panel-enter-from,
.user-panel-leave-to {
  opacity: 0;
  transform: scale(0.95);
}

/* 弹窗内容滚动条样式 */
.overflow-y-auto::-webkit-scrollbar {
  width: 6px;
}

.overflow-y-auto::-webkit-scrollbar-track {
  background: transparent;
}

.overflow-y-auto::-webkit-scrollbar-thumb {
  background: rgb(156 163 175 / 0.3);
  border-radius: 3px;
}

.overflow-y-auto::-webkit-scrollbar-thumb:hover {
  background: rgb(156 163 175 / 0.5);
}

/* 响应式设计 */
@media (max-width: 640px) {
  .max-w-md {
    max-width: calc(100vw - 2rem);
  }

  .grid-cols-3 {
    grid-template-columns: repeat(3, 1fr);
    gap: 0.5rem;
  }

  .text-2xl {
    font-size: 1.25rem;
  }
}
</style>
